package com.googlecode.worktools.utils;



import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * 用dom4j操作xml文件的工具类
 * 
 * @author Kobe Su,Jun 21, 2008
 */
public class Dom4jUtils {

	private static final Log log = LogFactory.getLog(Dom4jUtils.class);
	
	private Dom4jUtils() {

	}

	/**
     * 从输入流读取xml文档
	 * @param in 输入流
	 * @return Document对象
	 */
	public static Document readXml(InputStream in) {
		
		Document document = null;
		try {
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(in);
		} catch (DocumentException e) {
			log.error("读取xml文件失败："+e.getMessage(), e);
		} finally {
			close(in);
		}

		return document;
	}	
	
	/**
	 * 读取指定路径的xml文档
	 * @param path 文件路径
	 * @return Document对象，若找不到文件路径则返回null
	 */
	public static Document readXml(String path) {
		
		File file = new File(path);
		if(!file.exists())
			return null;
			
		Document document = null;
		try {
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(new File(path));
		} catch (DocumentException e) {
			log.error("读取xml文件["+path+"]失败："+e.getMessage(), e);
		}

		return document;
	}

	/**
	 * 将Document对象保存为一个xml文件到本地,默认GBK编码，Xml格式为压缩格式
	 * @param document 需要保存的document对象
	 * @param path 文件路径
	 */
	public static void writeXml(Document document, String path) {
		writeXml(document, path, "GBK", OutputFormat.createCompactFormat());
	}	
	
	/**
	 * 将Document对象保存为一个xml文件到本地
	 * @param document 需要保存的document对象
	 * @param path 文件路径
	 * @param encoding 编码
	 * @param format xml文档格式
	 */
	public static void writeXml(Document document, String path, String encoding, OutputFormat format) {
		try {
			format.setEncoding(encoding);
			XMLWriter writer = new XMLWriter(
					new FileWriter(new File(path)), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			log.error("Could not write Xml[" + path+ "]", e);
		}
	}	
	
	/**
	 * 将xml格式的字符串保存为一个xml文件到本地,默认GBK编码，Xml格式为压缩格式
	 * @param str xml格式的字符串
	 * @param path 文件路径
	 */
	public static void writeXml(String str, String path) {
		Document document = string2Document(str);
		writeXml(document, path, "GBK", OutputFormat.createCompactFormat());
	}
	
	/**
	 * 将xml格式的字符串保存为一个xml文件到本地
	 * @param str xml格式的字符串
	 * @param path 文件路径
	 * @param encoding 编码
	 * @param format xml文档格式
	 */
	public static void writeXml(String str, String path, String encoding, OutputFormat format) {
		Document document = string2Document(str);
		writeXml(document, path, encoding, format);
	}
		
	/**
	 * 将xml格式的字符串转为Document对象
	 * @param str xml格式的字符串
	 * @return Document对象
	 */
	public static Document string2Document(String str) {
		Document doc = null;

		try {
			doc = DocumentHelper.parseText(str);
		} catch (DocumentException e) {
			log.error("Could not parse string to Xml", e);
		}

		return doc;
	}

	/**
	 * 关闭输入流
	 * 
	 * @param in 输入流
	 */
	private static void close(InputStream in) {
		if (in == null) {
			return;
		}

		try {
			in.close();
		} catch (IOException e) {
			log.error(e);
		}
	}
	
	public static void main(String[] args) {

	}

}
